import Vue from 'vue';
import Vuex	from 'vuex';
import http from '@/common/api'

Vue.use(Vuex);
const info = {
	state: {
		config: '',
		location: '',
	},
	mutations: {
		setLocation(state,data){
			state.location = data
		},
		setConfig(state,data){
			state.config = data
		},
	},
	actions: {
		getLocation({
			commit
		}) {
			return new Promise((resolve, reject) => {
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						http.post('/getLocation', {
							lng: res.longitude,
							lat: res.latitude,
						}).then(response => {
							
							if (response.code == 1) {
								let province,city;
								province = response.data.province;
								if (response.data.city.length == 0) {
									city = response.data.province
								} else {
									city = response.data.city
								}
								let data = {
									province: province,
									city: city,
									district:response.data.district,
									lat: res.latitude,
									lng: res.longitude
								}
								
								commit('setLocation', data);
								resolve(data)
							}
						}).catch(e => {
							let data = {
								province: '河南省',
								city: '郑州市',
								lat: '34.74725',
								lng: '113.62493'
							}
							reject(data)
						})
					},
					fail:(e)=>{
						let data = {
							province: '河南省',
							city: '郑州市',
							lat: '34.74725',
							lng: '113.62493'
						}
						reject(data)
					}
				});
			})
		},
		getConfig({
			commit
		}) {
			return new Promise((resolve, reject) => {
				http.get('/same/index/getConfigure').then(res => {
					commit('setConfig', res.data);
					resolve(res.data)
				}).catch(err => {
					reject(err)
				})
			})
		}
	}
}
const store = new Vuex.Store({
	...info
});

export default store;